package cn.darkhorse.device.service.impl;

import cn.darkhorse.common.constant.CommonConstant;
import cn.darkhorse.device.mapper.MesSparePartsMapper;
import cn.darkhorse.device.pojo.MesSpareInboundInfo;
import cn.darkhorse.device.pojo.MesSpareParts;
import cn.darkhorse.device.pojo.MesSpareType;
import cn.darkhorse.device.service.MesSpareInboundInfoService;
import cn.darkhorse.device.service.MesSparePartsService;
import cn.darkhorse.device.service.MesSpareTypeService;
import cn.darkhorse.device.vo.MesSparePartsVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 备件信息表 服务实现类
 * </p>
 *
 * @author maoguanjun
 * @since 2022-11-11
 */
@Service
public class MesSparePartsServiceImpl extends ServiceImpl<MesSparePartsMapper, MesSpareParts> implements MesSparePartsService {

    @Resource
    private MesSpareTypeService mesSpareTypeService;
    @Resource
    private MesSpareInboundInfoService mesSpareInboundInfoService;
    @Resource
    private MesSparePartsMapper mesSparePartsMapper;
    /**
     * 列表
     *
     * @param name
     * @param id
     * @return
     */
    @Override
    public List<MesSparePartsVO> listAll(String name, String typeName, Long id) {
        List<MesSparePartsVO> voList = mesSparePartsMapper.listAll(name,typeName,id);

        return voList;
    }

    /**
     * 保存
     * @param spareParts
     * @return
     */
    @Override
    public boolean saveSpareParts(MesSpareParts spareParts) {
        LambdaQueryWrapper<MesSpareParts> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(MesSpareParts::getName,spareParts.getName());
        wrapper.eq(MesSpareParts::getModel,spareParts.getModel());
        if(null!=spareParts.getId()){
            wrapper.ne(MesSpareParts::getId,spareParts.getId());
            //存在入库记录
            if(mesSpareInboundInfoService.recordExists(spareParts.getId())){
                MesSpareParts parts=getById(spareParts.getId());
                parts.setMaker(spareParts.getMaker());
                parts.setIntroduce(spareParts.getIntroduce());
                parts.setForewarnInventory(spareParts.getForewarnInventory());
                parts.setAlertNotifier(spareParts.getAlertNotifier());
                BeanUtils.copyProperties(parts,spareParts);
            }
            spareParts.setGmtModify(LocalDateTime.now());
        }
        Long count=count(wrapper);
        if(count>0){
            throw new ServiceException("备件名称+规格/型号 已存在");
        }

        return saveOrUpdate(spareParts);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        LambdaQueryWrapper<MesSpareInboundInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(MesSpareInboundInfo::getSpareId,id);
        Long count=mesSpareInboundInfoService.count(wrapper);
        if(count>0){
            throw new ServiceException("备件存在入库记录，不可删除！");
        }
        return removeById(id);
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @Override
    public MesSparePartsVO view(Long id) {
        MesSpareParts mesSpareParts = getById(id);
        MesSparePartsVO mesSparePartsVO=new MesSparePartsVO();
        BeanUtils.copyProperties(mesSpareParts,mesSparePartsVO);
        if(null!=mesSpareParts){
            MesSpareType type=mesSpareTypeService.getById(mesSpareParts.getType());
            mesSparePartsVO.setTypeName(type.getName());
        }

        return mesSparePartsVO;
    }

    /**
     * 更新库存
     *
     * @param id
     * @param num
     * @param isAdd
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean upInventory(Long id, BigDecimal num, BigDecimal oldNum, boolean isAdd,Integer type) {
        MesSpareParts mesSpareParts = getById(id);
        if(isAdd){
            mesSpareParts.setInventory(mesSpareParts.getInventory().add(num).setScale(2, RoundingMode.HALF_UP));
        }else{
            if(mesSpareParts.getInventory().add(oldNum).compareTo(num)==-1){
                String msg="申请数量大于当前库存量！";
                //维修单
                if(CommonConstant.SPARE_APPLY_TYPE_REPAIR.equals(type)){
                    msg=String.format("%s备件申请数量大于库存量%s个",mesSpareParts.getName(),mesSpareParts.getInventory());
                }
                throw new ServiceException(msg);
            }else{
                mesSpareParts.setInventory(mesSpareParts.getInventory().add(oldNum).subtract(num).setScale(2, RoundingMode.HALF_UP));
            }
        }
        return  updateById(mesSpareParts);
    }
}
